
<!-- 消息处理组件 -->
{% include message/component/message_event.html %}

{% init tag = "todo" %}
{% init title = "待办任务" %}
{% init show_input_box = True %}
{% init message_stat = xutils.call("message.get_current_message_stat") %}

<div class="card">
    <div class="card-title">
        <span>{{title}}</span>
        <div class="float-right">
            {% include common/button/back_button.html %}
        </div>
    </div>
</div>

{% if tag != "done" %}
    <!-- 完成的页面不需要创建输入框 -->
    {% include message/component/message_input.html %}
{% end %}

{% if tag == "todo" or tag == "task" %}
    <div class="card">
        <a class="list-link" href="/message/done">
            <span>已完成任务</span>
            <div class="float-right">
                <span class="book-size-span">{{message_stat.done_count}}</span>
                <i class="fa fa-chevron-right"></i>
            </div>
        </a>
    </div>
{% end %}

<div class="col-md-12" id="todoItemList">
    <!-- 待办任务 AJAX加载 -->
</div>

<script type="text/javascript">
    $(function () {

        function loadTodoList() {
            var page = getUrlParam("page");
            if (page == undefined) {
                page = 1;
            }

            var params = {
                tag: "{{tag}}",
                format: "html",
                page: page,
            };
            $.get("/message/list", params, function (resp) {
                // console.log(resp);
                $("#todoItemList").html(resp);
            }).fail(function (e) {
                console.error(e);
                xnote.alert("调用接口失败，请重试");
            });
        }

        function onMessageRefresh(event) {
            loadTodoList();
            xnote.closeAllDialog();
        }

        function updateMessageTag(id, tag) {
            $.post("/message/tag", {id: id, tag: tag}, function (resp) {
                if (resp.code == "success") {
                    loadTodoList();
                } else {
                    alert(resp.message);
                }
            });
        }

        $("#todoItemList").on("click", ".todo-delete-btn", function (e) {
            var target = e.target;
            var id = $(target).attr("data-id");
            var content = $(target).attr("data-content");

            xnote.confirm("确认删除 '%s' ?".format(content), function (result) {
                $.post("/message/delete", {id: id}, function (resp) {
                    if (resp.code == "success") {
                        // 重新刷新页面
                        loadTodoList();
                    } else {
                        xnote.alert(data.message);
                    }
                });                    
            });
        });

        $(".select-topic-btn").click(function (e) {
            xnote.showIframeDialog("选择标签", "/message?op=select&tag=key");
        })



        // 监听上传事件
        xnote.on("message.refresh", onMessageRefresh);
        xnote.on("message.updated", onMessageRefresh);
        xnote.on("message.created", onMessageRefresh);

        // 加载todo列表
        loadTodoList();

        // 加载音频文件
        xnote.loadAudio("todo_done", "/static/audio/todo_done.mp3");
    });
</script>